import Unit
import Adsync


# 根据部门名获取部门ID
def getUnitName(unit_name):
    ops_obj = Unit.Unit()
    for tmp in ops_obj.getUnitList()['con']:
        if tmp['unit_name'] == unit_name:
            return tmp['unit_id']
            # print(tmp['unit_id'], tmp['unit_name'])

# 根据部门名获取一级部门ID
def getPriUnit(unit_name):
    ops_obj = Unit.Unit()
    for tmp in ops_obj.getUnitList()['con']:
        if tmp['unit_name'] == unit_name and 'parent_id' not in tmp:
            return tmp['unit_id']


# 根据一级部门ID获取末级部门ID
# {'con': [{'parent_id': '769182', 'unit_id': '1112884', 'unit_name': '综合管理', 'unit_rank': 99991}, ...], 'suc': True, 'ver': 1635762816691}
def getSubUnit(unit_id, sudept_list):
    ops_obj = Unit.Unit()
    # print(ops_obj.getSubUnitList(unit_id, 'false')['con'])
    if ops_obj.getSubUnitList(unit_id, 'false')['con']:
        for tmp in ops_obj.getSubUnitList(unit_id, 'false')['con']:
            if ops_obj.getSubUnitList(tmp['unit_id'], 'false')['con']:
                getSubUnit(tmp['unit_id'], sudept_list)
            else:
                sudept_list.append(ops_obj.getUnit(tmp['unit_id'])['con']['unit_id'] + '+' + ops_obj.getUnit(tmp['unit_id'])['con']['unit_name'])
                # print(tmp['unit_id'], ops_obj.getUnit(tmp['unit_id']))
    else:
        sudept_list.append(ops_obj.getUnit('unit_id')['con']['unit_id'] + '+' + ops_obj.getUnit('unit_id')['con']['unit_name'])
        # print(unit_id)


if __name__ == '__main__':
    # newcommer_obj = Adsync.Adsync()
    # for tmp in newcommer_obj:
    #     print(tmp['dept'] + ':')
    #     getUnitName(tmp['dept'])
    print(getPriUnit('3C事业部'))
    sudept_list = []
    # getSubUnit('873789')
    getSubUnit('1006318', sudept_list)
    # 获取一级部门所有末级部门
    print(sudept_list)
